package chloroform.json.comp

import java.util.Date
import chloroform.json.JsonBuilderFactory
import util.ColUtils
import argo.jdom.{JsonRootNode, JsonNode}

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 15.5.11
 * Time: 13:45
 */
object Components {

}

class Component(objectType: String) extends JsonComposite {
  val otype = string(objectType)
  val id = string()
}

class Field(objectType: String, fieldName: String, valueClass: String) extends Component(objectType) {
  val name = string(fieldName)
  val label = string(fieldName)
  val clazz = string(valueClass)
}

class StringField(fieldName: String, v: String) extends Field("StringField", fieldName, "java.lang.String") {
  val value = string(v)
}

class BooleanField(fieldName: String, v: Boolean) extends Field("BooleanField", fieldName, "java.lang.Boolean") {
  val value = boolean(v)
}

class NumberField(fieldName: String, numberClass: String, v: Number) extends Field("NumberField", fieldName, numberClass) {
  val decimalPrecision = number()
  val value = number(v)

  if (numberClass == "java.lang.Byte" ||
    numberClass == "java.lang.Short" ||
    numberClass == "java.lang.Integer" ||
    numberClass == "java.lang.Long")
    decimalPrecision <= 0
  else if (numberClass == "java.lang.Float")
    decimalPrecision <= 7
  else if (numberClass == "java.lang.Double")
    decimalPrecision <= 60
  else
    decimalPrecision <= 0

}

class DateField(fieldName: String, v: Date) extends Field("DateField", fieldName, "java.util.Date") {
  val value = date(v)
}

import ColUtils._

object ArrayField {
  def apply(fieldName: String, v: java.util.List[String]): ArrayField = {
    val scalaList: List[String] = v;
    new ArrayField(fieldName, scalaList)
  }
}

class ArrayField(fieldName: String, v: List[String]) extends Field("ArrayField", fieldName, "java.lang.Array") {
  val value = array(string())
  value <= v
}


class AbstractItem(itemId: String, txt: String, otype: String) extends Component(otype) {
  val text = string(txt)
  id <= itemId
}


class Item(itemId: String, txt: String) extends AbstractItem(itemId, txt, "Item")

class CheckItem(itemId: String, txt: String) extends AbstractItem(itemId, txt, "CheckItem") {
  val checked = boolean()
}

//class ProjectEntry(entryId: String, typeOfEntry: String, extFactories: Map[String, JsonBuilderFactory[JsonNode]])
class ProjectEntry(entryId: String, typeOfEntry: String, extFactories: List[JsonBuilderFactory[JsonRootNode]])
  extends JsonComposite {

  val id = string(entryId)
  val entryType = string(typeOfEntry)
  //val extensions = new MapJsonComposite(extFactories)
  val extensions = array(struct())
  extensions <= extFactories
}
